import { defineStore } from 'pinia'
import { checklistAnalysePageApi, checklistChecklistPageApi } from '@/api/01work/01stage'
export const checkAnalysisStore = defineStore('checkAnalysisStore', {
  state: () => {
    return {
      checkList: [] as any,// 通过阶段查询分析名称 =>
      analysisList: [] as any,// 通过分析名称查询检查单
      selectList: [] as any
    }
  },
  actions: {
    // 可以直接获取数据 1 一个是分析项目 2 一个是检查单 两个不一样的嘛
    async getCheck(id) {
      // 分析项目
      console.log('分析项目',id);
      
      const res = await checklistAnalysePageApi(id)
      if (res.code === 200) {
        this.checkList = res.data
        if (res.data.length > 0) {
          this.getAnalysis(res.data[0].id)
          this.selectList = [res.data[0]]
        }
      }
    },
    async getAnalysis(id) {
      const res = await checklistChecklistPageApi({ analyseId: id })
      if (res.code === 200) {
        this.analysisList = res.data
      }
    },
    setCheckAllList(row) {
      this.getAnalysis(row.id)
      // 第一个是默认 第二个是从新选择
      const list = this.checkList
      const index = list.findIndex(f => f.id === row.id)
      if (index === -1) {
        this.selectList = [row]
      } else {
        this.selectList = [this.checkList[index]]
      }
    }
  },
  getters: {
    getChecks(state) {
      return state.checkList
    },
    getAnalysiss(state) {
      return state.analysisList
    },
    getCheckLists(state) {
      return state.selectList
    }
  }
})